package com.generator.statics.manufacturerPurchaseStatics.controller;


import com.generator.statics.manufacturerPurchaseStatics.pojo.CPurchase1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.CPurchaseCon1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.City1;
import com.generator.statics.manufacturerPurchaseStatics.pojo.Province1;
import com.generator.statics.manufacturerPurchaseStatics.service.PurchaseService;
import com.generator.util.PageModel;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.util.List;
import java.util.Objects;

@Controller
@RequestMapping("/manuPurchase")
public class PurchaseAction {

    @Autowired
    private PurchaseService purchaseService;

    public PurchaseService getPurchaseService() {
        return purchaseService;
    }

    public void setPurchaseService(PurchaseService purchaseService) {
        this.purchaseService = purchaseService;
    }

    // 查询所有厂商采购记录
    @RequestMapping("/allPurchase")
    public String allPurchase(Model model, CPurchase1 cPurchase, PageModel<CPurchaseCon1> pageModel, HttpSession session) {
        PageModel<CPurchaseCon1> purchasePage = purchaseService.getPurchaseListByCondition(cPurchase, pageModel);
        model.addAttribute("purchasePage", purchasePage);
        return "forward:/manuPurchase/jumpPage";
    }

    // 跳转到purchaseStatis页面
    @RequestMapping("/jumpPage")
    public String jumpPage(Model model) {
        return "statis/purchase/purchaseStatis";
    }

    //    查看单个厂商采购记录
    @RequestMapping("/onePurchase/{id}")
    public String onePurchase(@PathVariable("id") String id, Model model) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setPurchase_id(id);

        List<CPurchaseCon1> purchase = purchaseService.onePurchase(conn);
        System.out.println(purchase);

        model.addAttribute("purchase", purchase);
        return "statis/purchase/purchaseView";

    }

    //    查看单个厂商详情采购记录
    @RequestMapping("/oneDetailedPurchase/{purchaseId}")
    public String oneDetailedPurchase(@PathVariable("purchaseId") String purchaseId, Model model) throws UnsupportedEncodingException {

        CPurchaseCon1 conn = new CPurchaseCon1();


        conn.setPurchase_id(purchaseId);


        List<CPurchaseCon1> cDetailedPurchases = purchaseService.oneDetailedPurchase(conn);

        CPurchaseCon1 cDetailedPurchasesDown = purchaseService.oneDetailedPurchaseDown(conn);


        System.out.println("cDetailedPurchases.get(0): " + cDetailedPurchases.get(0));


        System.out.println("cDetailedPurchasesDown: " + cDetailedPurchasesDown);

        model.addAttribute("cDetailedPurchases", cDetailedPurchases.get(0));
        model.addAttribute("cDetailedPurchasesDown", cDetailedPurchasesDown);

        return "purchase/purchase/purchaseView";
    }

    // 厂商采购记录分类查询
    @RequestMapping("/vaguePurchaseSelect")
    public String classQueryPurchaseListByCondition(@Param("firmName") String firmName, @Param("sheng") long sheng,
                                                    @Param("shi") long shi, Model model, PageModel<CPurchase1> pageModel) {

        CPurchaseCon1 conn = new CPurchaseCon1();
        conn.setFirmName(firmName);

        if (Objects.equals(firmName, "%") || Objects.equals(firmName, "_")){
            conn.setFirmName(")(@#~【】");
        }else {
            conn.setFirmName(firmName);
        }

        Province1 province = new Province1();
        City1 city = new City1();
        if (Objects.equals(shi, "0")){
            province.setId(sheng);
            String provinces = purchaseService.queryProvince(province);
            conn.setFirmAddress(provinces);
        }else{
            city.setId(shi);
            String city1 = purchaseService.queryCity(city);
            conn.setFirmAddress(city1);
        }

        PageModel<CPurchase1> purchasePage = purchaseService.classQueryPurchaseListByCondition(conn, pageModel);

        model.addAttribute("purchasePage", purchasePage);
        return "forward:/manuPurchase/jumpPage";
    }

    // 厂商采购记录列表（分页）
    @RequestMapping("/purchaseList")
    public String userList(Model model, CPurchase1 user, PageModel<CPurchaseCon1> pageModel){
        PageModel<CPurchaseCon1> userList = purchaseService.getPurchaseListByCondition(user,pageModel);
        model.addAttribute("purchasePage",userList);
        return "forward:/manuPurchase/jumpPage";
    }


}
